layui.use(['layer', 'form', 'table'], function () {
    var layer = layui.layer,
        $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        t;                  //表格数据变量

    t = {
        elem: '#deviceTable',
        url: '/device/findAllDevices',
        method: 'post',
        page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
            //,curr: 5 //设定初始在第 5 页
            groups: 6, //只显示 1 个连续页码
            first: "首页", //显示首页
            last: "尾页", //显示尾页
            limits: [3, 10, 20, 30]
        },
        width: $(parent.window).width() - 223,
        cols: [[
            {type: 'checkbox'},
            {field: 'deviceName', title: '设备名称', width: '10%'},
            {field: 'deviceIp', title: 'IP', width: '10%'},
            {field: 'deviceSn', title: 'sn码', width: '10%'},
            {field: 'deviceType', title: '设备类型',width: '10%'},
            {field: 'deviceInstalltime', title: '设备安装日期', width: '10%'},
            {field: 'deviceProtime', title: '设备生产日期', width: '10%'},
            {field: 'deviceState', title: '设备状态', width: '10%', templet: '#deviceStatus'},
            {
                field: 'createTime',
                title: '创建时间',
                width: '15%',
                templet: '<span>{{ layui.laytpl.toDateString(d.createTime) }}</span>'
            }, //单元格内容水平居中
            {fixed: 'right', title: '操作', align: 'center', toolbar: '#deviceBar'}
        ]]
    };
    table.render(t);

    //监听工具条
    table.on('tool(deviceList)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            var editIndex = layer.open({
                title: "编辑设备",
                type: 2,
                content: "/device/edit?id=" + data.id,
                success: function (layero, index) {
                    setTimeout(function () {
                        layer.tips('点击此处返回设备列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500);
                }
            });
            //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
            $(window).resize(function () {
                layer.full(editIndex);
            });
            layer.full(editIndex);
        }

        if (obj.event === "del") {
            layer.confirm("你确定要删除该设备么？", {btn: ['是的,我确定', '我再想想']},
                function () {
                    $.post("/device/updateDevice", {"id": data.id}, function (res) {
                        if (res.code === 0) {
                            layer.msg("删除成功", {time: 1000}, function () {
                                table.reload('deviceTable', t);
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }
            );
        }
    });

    //功能按钮
    var active = {
        addDevice: function () {
            var addIndex = layer.open({
                title: "添加设备",
                type: 2,
                content: "/device/add",
                success: function (layero, addIndex) {
                    setTimeout(function () {
                        layer.tips('点击此处返回会员列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500);
                }
            });
            //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
            $(window).resize(function () {
                layer.full(addIndex);
            });
            layer.full(addIndex);
        },
        //批量删除
        deleteSome: function () {
            let checkStatus = table.checkStatus('deviceTable'),
                data = checkStatus.data;
            if (data.length > 0) {
                for (let i = 0; i < data.length; i++) {
                    let d = data[i];
                    if (d.adminbook) {
                        layer.msg("不能删除超级管理员");
                        return false;
                    }
                }
                let ids = [];
                data.forEach(function (i, index) {
                    ids.push(i.id);
                })
                layer.confirm("你确定要删除这些设备么？", {btn: ['是的,我确定', '我再想想']},
                    function () {
                        let deleteindex = layer.msg('删除中，请稍候', {icon: 16, time: false, shade: 0.8});
                        $.ajax({
                            type: "POST",
                            url: "/device/delDeviceAll",
                            data: {ids: ids.join()},
                            success: function (res) {
                                layer.close(deleteindex);
                                if (res.code === 0) {
                                    layer.msg("删除成功", {time: 1000}, function () {
                                        table.reload('deviceTable', t);
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            }
                        });
                    }
                )
            } else {
                layer.msg("请选择需要删除的设备", {time: 1000});
            }
        }
    };

    $('.layui-inline .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //搜索
    form.on("submit(searchForm)", function (data) {
        t.where = data.field;
        table.reload('deviceTable', t);
        return false;
    });


});